import {
	createRouter,
	createWebHashHistory
} from 'vue-router';

const routes = [{
		name: 'notFound',
		path: '/:path(.*)+',
		redirect: {
			name: 'Home',
		},
	},
	{
		name: 'Home',
		path: '/home',
		component: () => import('../view/home/index.vue')
	},
	{
		path: '/login',
		name: 'Login',
		component: () => import('../view/login/index.vue')
	},
	{
		path: '/register',
		name: 'Register',
		component: () => import('../view/register/index.vue')
	},
	{
		name: 'User',
		path: '/user',
		component: () => import('../view/user/index.vue'),
		meta: {
			title: '会员中心',
		},
	},
	{
		name: 'Cart',
		path: '/cart',
		component: () => import('../view/cart/index.vue'),
		meta: {
			title: '购物车',
		},
	},
	{
		name: 'Goods',
		path: '/goods/:id',
		component: () => import('../view/goods/index.vue'),
		meta: {
			title: '商品详情',
		},
	},
	{
		name: 'Detail',
		path: '/detail/:id',
		component: () => import('../view/goods/Detail.vue')
	},
];

const router = createRouter({
	routes,
	history: createWebHashHistory(),
});

router.beforeEach((to, from, next) => {
	const title = to.meta && to.meta.title;
	if (title) {
		document.title = title;
	}
	next();
});

export default router